{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入的csv文件，是中国股票市场上证指数000001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.0500</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>95.7900</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>126000</td>\n",
       "      <td>4.940000e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.3000</td>\n",
       "      <td>104.3900</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>104.3900</td>\n",
       "      <td>19700</td>\n",
       "      <td>8.400000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.0700</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>103.7300</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>2800</td>\n",
       "      <td>1.600000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.5700</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>3200</td>\n",
       "      <td>3.100000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.0900</td>\n",
       "      <td>120.2500</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>120.2500</td>\n",
       "      <td>1500</td>\n",
       "      <td>6.000000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8210 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1990-12-19               96.0500    99.9800    95.7900    99.9800   \n",
       "1990-12-20      99.98   104.3000   104.3900    99.9800   104.3900   \n",
       "1990-12-21     104.39   109.0700   109.1300   103.7300   109.1300   \n",
       "1990-12-24     109.13   113.5700   114.5500   109.1300   114.5500   \n",
       "1990-12-25     114.55   120.0900   120.2500   114.5500   120.2500   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money  \n",
       "Day                                    \n",
       "1990-12-19       126000  4.940000e+05  \n",
       "1990-12-20        19700  8.400000e+04  \n",
       "1990-12-21         2800  1.600000e+04  \n",
       "1990-12-24         3200  3.100000e+04  \n",
       "1990-12-25         1500  6.000000e+03  \n",
       "...                 ...           ...  \n",
       "2024-07-25  27463950000  2.732820e+11  \n",
       "2024-07-26  27838753600  2.754430e+11  \n",
       "2024-07-29  25689972700  2.600950e+11  \n",
       "2024-07-30  26247883700  2.694770e+11  \n",
       "2024-07-31  41272341700  4.188720e+11  \n",
       "\n",
       "[8210 rows x 7 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('D:/ruo-ning/000001.csv')\n",
    "data['Day']=pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "data.set_index('Day',inplace=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-01</th>\n",
       "      <td>641.14</td>\n",
       "      <td>631.5500</td>\n",
       "      <td>639.4900</td>\n",
       "      <td>624.9300</td>\n",
       "      <td>633.8100</td>\n",
       "      <td>92195100</td>\n",
       "      <td>4.617370e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-04</th>\n",
       "      <td>633.81</td>\n",
       "      <td>632.7200</td>\n",
       "      <td>635.7300</td>\n",
       "      <td>632.2400</td>\n",
       "      <td>634.9200</td>\n",
       "      <td>41221800</td>\n",
       "      <td>2.151010e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-05</th>\n",
       "      <td>634.92</td>\n",
       "      <td>636.4300</td>\n",
       "      <td>638.7600</td>\n",
       "      <td>635.5900</td>\n",
       "      <td>637.2200</td>\n",
       "      <td>46021200</td>\n",
       "      <td>2.247910e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-06</th>\n",
       "      <td>637.22</td>\n",
       "      <td>637.3000</td>\n",
       "      <td>637.3200</td>\n",
       "      <td>627.8500</td>\n",
       "      <td>628.4300</td>\n",
       "      <td>64182100</td>\n",
       "      <td>4.021530e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-07</th>\n",
       "      <td>628.43</td>\n",
       "      <td>628.6300</td>\n",
       "      <td>632.3700</td>\n",
       "      <td>627.0600</td>\n",
       "      <td>631.8400</td>\n",
       "      <td>50083200</td>\n",
       "      <td>2.993620e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6952 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1995-12-01     641.14   631.5500   639.4900   624.9300   633.8100   \n",
       "1995-12-04     633.81   632.7200   635.7300   632.2400   634.9200   \n",
       "1995-12-05     634.92   636.4300   638.7600   635.5900   637.2200   \n",
       "1995-12-06     637.22   637.3000   637.3200   627.8500   628.4300   \n",
       "1995-12-07     628.43   628.6300   632.3700   627.0600   631.8400   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money  \n",
       "Day                                    \n",
       "1995-12-01     92195100  4.617370e+08  \n",
       "1995-12-04     41221800  2.151010e+08  \n",
       "1995-12-05     46021200  2.247910e+08  \n",
       "1995-12-06     64182100  4.021530e+08  \n",
       "1995-12-07     50083200  2.993620e+08  \n",
       "...                 ...           ...  \n",
       "2024-07-25  27463950000  2.732820e+11  \n",
       "2024-07-26  27838753600  2.754430e+11  \n",
       "2024-07-29  25689972700  2.600950e+11  \n",
       "2024-07-30  26247883700  2.694770e+11  \n",
       "2024-07-31  41272341700  4.188720e+11  \n",
       "\n",
       "[6952 rows x 7 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new = data['1995-12':'2024-07'].copy()\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-01</th>\n",
       "      <td>641.1400</td>\n",
       "      <td>631.5500</td>\n",
       "      <td>639.4900</td>\n",
       "      <td>624.9300</td>\n",
       "      <td>633.8100</td>\n",
       "      <td>92195100</td>\n",
       "      <td>4.617370e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-04</th>\n",
       "      <td>633.8100</td>\n",
       "      <td>632.7200</td>\n",
       "      <td>635.7300</td>\n",
       "      <td>632.2400</td>\n",
       "      <td>634.9200</td>\n",
       "      <td>41221800</td>\n",
       "      <td>2.151010e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-05</th>\n",
       "      <td>634.9200</td>\n",
       "      <td>636.4300</td>\n",
       "      <td>638.7600</td>\n",
       "      <td>635.5900</td>\n",
       "      <td>637.2200</td>\n",
       "      <td>46021200</td>\n",
       "      <td>2.247910e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-06</th>\n",
       "      <td>637.2200</td>\n",
       "      <td>637.3000</td>\n",
       "      <td>637.3200</td>\n",
       "      <td>627.8500</td>\n",
       "      <td>628.4300</td>\n",
       "      <td>64182100</td>\n",
       "      <td>4.021530e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-07</th>\n",
       "      <td>628.4300</td>\n",
       "      <td>628.6300</td>\n",
       "      <td>632.3700</td>\n",
       "      <td>627.0600</td>\n",
       "      <td>631.8400</td>\n",
       "      <td>50083200</td>\n",
       "      <td>2.993620e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6952 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1995-12-01   641.1400   631.5500   639.4900   624.9300   633.8100   \n",
       "1995-12-04   633.8100   632.7200   635.7300   632.2400   634.9200   \n",
       "1995-12-05   634.9200   636.4300   638.7600   635.5900   637.2200   \n",
       "1995-12-06   637.2200   637.3000   637.3200   627.8500   628.4300   \n",
       "1995-12-07   628.4300   628.6300   632.3700   627.0600   631.8400   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money  \n",
       "Day                                    \n",
       "1995-12-01     92195100  4.617370e+08  \n",
       "1995-12-04     41221800  2.151010e+08  \n",
       "1995-12-05     46021200  2.247910e+08  \n",
       "1995-12-06     64182100  4.021530e+08  \n",
       "1995-12-07     50083200  2.993620e+08  \n",
       "...                 ...           ...  \n",
       "2024-07-25  27463950000  2.732820e+11  \n",
       "2024-07-26  27838753600  2.754430e+11  \n",
       "2024-07-29  25689972700  2.600950e+11  \n",
       "2024-07-30  26247883700  2.694770e+11  \n",
       "2024-07-31  41272341700  4.188720e+11  \n",
       "\n",
       "[6952 rows x 7 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new=data['1995-12':'2024-07'].copy()\n",
    "data_new['Close']=pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose']=pd.to_numeric(data_new['Preclose'])\n",
    "data_new"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量化的处理数据方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Reyurn_2</th>\n",
       "      <th>Return_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-01</th>\n",
       "      <td>641.1400</td>\n",
       "      <td>631.5500</td>\n",
       "      <td>639.4900</td>\n",
       "      <td>624.9300</td>\n",
       "      <td>633.8100</td>\n",
       "      <td>92195100</td>\n",
       "      <td>4.617370e+08</td>\n",
       "      <td>-0.011433</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-04</th>\n",
       "      <td>633.8100</td>\n",
       "      <td>632.7200</td>\n",
       "      <td>635.7300</td>\n",
       "      <td>632.2400</td>\n",
       "      <td>634.9200</td>\n",
       "      <td>41221800</td>\n",
       "      <td>2.151010e+08</td>\n",
       "      <td>0.001751</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-05</th>\n",
       "      <td>634.9200</td>\n",
       "      <td>636.4300</td>\n",
       "      <td>638.7600</td>\n",
       "      <td>635.5900</td>\n",
       "      <td>637.2200</td>\n",
       "      <td>46021200</td>\n",
       "      <td>2.247910e+08</td>\n",
       "      <td>0.003623</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-06</th>\n",
       "      <td>637.2200</td>\n",
       "      <td>637.3000</td>\n",
       "      <td>637.3200</td>\n",
       "      <td>627.8500</td>\n",
       "      <td>628.4300</td>\n",
       "      <td>64182100</td>\n",
       "      <td>4.021530e+08</td>\n",
       "      <td>-0.013794</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-07</th>\n",
       "      <td>628.4300</td>\n",
       "      <td>628.6300</td>\n",
       "      <td>632.3700</td>\n",
       "      <td>627.0600</td>\n",
       "      <td>631.8400</td>\n",
       "      <td>50083200</td>\n",
       "      <td>2.993620e+08</td>\n",
       "      <td>0.005426</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6952 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1995-12-01   641.1400   631.5500   639.4900   624.9300   633.8100   \n",
       "1995-12-04   633.8100   632.7200   635.7300   632.2400   634.9200   \n",
       "1995-12-05   634.9200   636.4300   638.7600   635.5900   637.2200   \n",
       "1995-12-06   637.2200   637.3000   637.3200   627.8500   628.4300   \n",
       "1995-12-07   628.4300   628.6300   632.3700   627.0600   631.8400   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money    Return  Reyurn_2  Return_2  \n",
       "Day                                                                  \n",
       "1995-12-01     92195100  4.617370e+08 -0.011433         0         0  \n",
       "1995-12-04     41221800  2.151010e+08  0.001751         0         0  \n",
       "1995-12-05     46021200  2.247910e+08  0.003623         0         0  \n",
       "1995-12-06     64182100  4.021530e+08 -0.013794         0         0  \n",
       "1995-12-07     50083200  2.993620e+08  0.005426         0         0  \n",
       "...                 ...           ...       ...       ...       ...  \n",
       "2024-07-25  27463950000  2.732820e+11 -0.005241         0         0  \n",
       "2024-07-26  27838753600  2.754430e+11  0.001440         0         0  \n",
       "2024-07-29  25689972700  2.600950e+11  0.000328         0         0  \n",
       "2024-07-30  26247883700  2.694770e+11 -0.004338         0         0  \n",
       "2024-07-31  41272341700  4.188720e+11  0.020647         0         0  \n",
       "\n",
       "[6952 rows x 10 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return']=(data_new['Close'])/data_new['Preclose']-1\n",
    "data_new['Return_2']=0\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\宁\\AppData\\Local\\Temp\\ipykernel_9036\\344095362.py:2: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i] / data_new['Preclose'][i])-1\n",
      "C:\\Users\\宁\\AppData\\Local\\Temp\\ipykernel_9036\\344095362.py:2: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i] / data_new['Preclose'][i])-1\n",
      "C:\\Users\\宁\\AppData\\Local\\Temp\\ipykernel_9036\\344095362.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i] / data_new['Preclose'][i])-1\n",
      "C:\\Users\\宁\\AppData\\Local\\Temp\\ipykernel_9036\\344095362.py:2: FutureWarning: Series.__setitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To set a value by position, use `ser.iloc[pos] = value`\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i] / data_new['Preclose'][i])-1\n",
      "C:\\Users\\宁\\AppData\\Local\\Temp\\ipykernel_9036\\344095362.py:2: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '-0.011432760395545527' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i] / data_new['Preclose'][i])-1\n",
      "C:\\Users\\宁\\AppData\\Local\\Temp\\ipykernel_9036\\344095362.py:2: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i] / data_new['Preclose'][i])-1\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Reyurn_2</th>\n",
       "      <th>Return_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-01</th>\n",
       "      <td>641.1400</td>\n",
       "      <td>631.5500</td>\n",
       "      <td>639.4900</td>\n",
       "      <td>624.9300</td>\n",
       "      <td>633.8100</td>\n",
       "      <td>92195100</td>\n",
       "      <td>4.617370e+08</td>\n",
       "      <td>-0.011433</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.011433</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-04</th>\n",
       "      <td>633.8100</td>\n",
       "      <td>632.7200</td>\n",
       "      <td>635.7300</td>\n",
       "      <td>632.2400</td>\n",
       "      <td>634.9200</td>\n",
       "      <td>41221800</td>\n",
       "      <td>2.151010e+08</td>\n",
       "      <td>0.001751</td>\n",
       "      <td>0</td>\n",
       "      <td>0.001751</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-05</th>\n",
       "      <td>634.9200</td>\n",
       "      <td>636.4300</td>\n",
       "      <td>638.7600</td>\n",
       "      <td>635.5900</td>\n",
       "      <td>637.2200</td>\n",
       "      <td>46021200</td>\n",
       "      <td>2.247910e+08</td>\n",
       "      <td>0.003623</td>\n",
       "      <td>0</td>\n",
       "      <td>0.003623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-06</th>\n",
       "      <td>637.2200</td>\n",
       "      <td>637.3000</td>\n",
       "      <td>637.3200</td>\n",
       "      <td>627.8500</td>\n",
       "      <td>628.4300</td>\n",
       "      <td>64182100</td>\n",
       "      <td>4.021530e+08</td>\n",
       "      <td>-0.013794</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.013794</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-07</th>\n",
       "      <td>628.4300</td>\n",
       "      <td>628.6300</td>\n",
       "      <td>632.3700</td>\n",
       "      <td>627.0600</td>\n",
       "      <td>631.8400</td>\n",
       "      <td>50083200</td>\n",
       "      <td>2.993620e+08</td>\n",
       "      <td>0.005426</td>\n",
       "      <td>0</td>\n",
       "      <td>0.005426</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.005241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0</td>\n",
       "      <td>0.001440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.004338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0</td>\n",
       "      <td>0.020647</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6952 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1995-12-01   641.1400   631.5500   639.4900   624.9300   633.8100   \n",
       "1995-12-04   633.8100   632.7200   635.7300   632.2400   634.9200   \n",
       "1995-12-05   634.9200   636.4300   638.7600   635.5900   637.2200   \n",
       "1995-12-06   637.2200   637.3000   637.3200   627.8500   628.4300   \n",
       "1995-12-07   628.4300   628.6300   632.3700   627.0600   631.8400   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money    Return  Reyurn_2  Return_2  \n",
       "Day                                                                  \n",
       "1995-12-01     92195100  4.617370e+08 -0.011433         0 -0.011433  \n",
       "1995-12-04     41221800  2.151010e+08  0.001751         0  0.001751  \n",
       "1995-12-05     46021200  2.247910e+08  0.003623         0  0.003623  \n",
       "1995-12-06     64182100  4.021530e+08 -0.013794         0 -0.013794  \n",
       "1995-12-07     50083200  2.993620e+08  0.005426         0  0.005426  \n",
       "...                 ...           ...       ...       ...       ...  \n",
       "2024-07-25  27463950000  2.732820e+11 -0.005241         0 -0.005241  \n",
       "2024-07-26  27838753600  2.754430e+11  0.001440         0  0.001440  \n",
       "2024-07-29  25689972700  2.600950e+11  0.000328         0  0.000328  \n",
       "2024-07-30  26247883700  2.694770e+11 -0.004338         0 -0.004338  \n",
       "2024-07-31  41272341700  4.188720e+11  0.020647         0  0.020647  \n",
       "\n",
       "[6952 rows x 10 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(0,len(data_new)):\n",
    "    data_new['Return_2'][i]=(data_new['Close'][i] / data_new['Preclose'][i])-1\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Reyurn_2</th>\n",
       "      <th>Return_2</th>\n",
       "      <th>diff</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-01</th>\n",
       "      <td>641.1400</td>\n",
       "      <td>631.5500</td>\n",
       "      <td>639.4900</td>\n",
       "      <td>624.9300</td>\n",
       "      <td>633.8100</td>\n",
       "      <td>92195100</td>\n",
       "      <td>4.617370e+08</td>\n",
       "      <td>-0.011433</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.011433</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-04</th>\n",
       "      <td>633.8100</td>\n",
       "      <td>632.7200</td>\n",
       "      <td>635.7300</td>\n",
       "      <td>632.2400</td>\n",
       "      <td>634.9200</td>\n",
       "      <td>41221800</td>\n",
       "      <td>2.151010e+08</td>\n",
       "      <td>0.001751</td>\n",
       "      <td>0</td>\n",
       "      <td>0.001751</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-05</th>\n",
       "      <td>634.9200</td>\n",
       "      <td>636.4300</td>\n",
       "      <td>638.7600</td>\n",
       "      <td>635.5900</td>\n",
       "      <td>637.2200</td>\n",
       "      <td>46021200</td>\n",
       "      <td>2.247910e+08</td>\n",
       "      <td>0.003623</td>\n",
       "      <td>0</td>\n",
       "      <td>0.003623</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-06</th>\n",
       "      <td>637.2200</td>\n",
       "      <td>637.3000</td>\n",
       "      <td>637.3200</td>\n",
       "      <td>627.8500</td>\n",
       "      <td>628.4300</td>\n",
       "      <td>64182100</td>\n",
       "      <td>4.021530e+08</td>\n",
       "      <td>-0.013794</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.013794</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-07</th>\n",
       "      <td>628.4300</td>\n",
       "      <td>628.6300</td>\n",
       "      <td>632.3700</td>\n",
       "      <td>627.0600</td>\n",
       "      <td>631.8400</td>\n",
       "      <td>50083200</td>\n",
       "      <td>2.993620e+08</td>\n",
       "      <td>0.005426</td>\n",
       "      <td>0</td>\n",
       "      <td>0.005426</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6952 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1995-12-01   641.1400   631.5500   639.4900   624.9300   633.8100   \n",
       "1995-12-04   633.8100   632.7200   635.7300   632.2400   634.9200   \n",
       "1995-12-05   634.9200   636.4300   638.7600   635.5900   637.2200   \n",
       "1995-12-06   637.2200   637.3000   637.3200   627.8500   628.4300   \n",
       "1995-12-07   628.4300   628.6300   632.3700   627.0600   631.8400   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money    Return  Reyurn_2  Return_2  diff  \n",
       "Day                                                                        \n",
       "1995-12-01     92195100  4.617370e+08 -0.011433         0 -0.011433   0.0  \n",
       "1995-12-04     41221800  2.151010e+08  0.001751         0  0.001751   0.0  \n",
       "1995-12-05     46021200  2.247910e+08  0.003623         0  0.003623   0.0  \n",
       "1995-12-06     64182100  4.021530e+08 -0.013794         0 -0.013794   0.0  \n",
       "1995-12-07     50083200  2.993620e+08  0.005426         0  0.005426   0.0  \n",
       "...                 ...           ...       ...       ...       ...   ...  \n",
       "2024-07-25  27463950000  2.732820e+11 -0.005241         0 -0.005241   0.0  \n",
       "2024-07-26  27838753600  2.754430e+11  0.001440         0  0.001440   0.0  \n",
       "2024-07-29  25689972700  2.600950e+11  0.000328         0  0.000328   0.0  \n",
       "2024-07-30  26247883700  2.694770e+11 -0.004338         0 -0.004338   0.0  \n",
       "2024-07-31  41272341700  4.188720e+11  0.020647         0  0.020647   0.0  \n",
       "\n",
       "[6952 rows x 11 columns]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['diff']=data_new['Return']-data_new['Return_2']\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "两种方法计算的差异： Day\n",
      "1995-12-01    0.0\n",
      "1995-12-04    0.0\n",
      "1995-12-05    0.0\n",
      "1995-12-06    0.0\n",
      "1995-12-07    0.0\n",
      "             ... \n",
      "2024-07-25    0.0\n",
      "2024-07-26    0.0\n",
      "2024-07-29    0.0\n",
      "2024-07-30    0.0\n",
      "2024-07-31    0.0\n",
      "Name: diff, Length: 6952, dtype: float64 <built-in function sum>\n"
     ]
    }
   ],
   "source": [
    "print('两种方法计算的差异：',data_new['diff'],sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Reyurn_2</th>\n",
       "      <th>Return_2</th>\n",
       "      <th>diff</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-12-16</th>\n",
       "      <td>1110.04</td>\n",
       "      <td>1005.01</td>\n",
       "      <td>1005.12</td>\n",
       "      <td>999.63</td>\n",
       "      <td>1000.02</td>\n",
       "      <td>143300300</td>\n",
       "      <td>1.378386e+09</td>\n",
       "      <td>-0.099114</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.099114</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-17</th>\n",
       "      <td>1000.02</td>\n",
       "      <td>907.65</td>\n",
       "      <td>943.37</td>\n",
       "      <td>903.85</td>\n",
       "      <td>905.58</td>\n",
       "      <td>601468400</td>\n",
       "      <td>5.295694e+09</td>\n",
       "      <td>-0.094438</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.094438</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-02-18</th>\n",
       "      <td>982.40</td>\n",
       "      <td>980.48</td>\n",
       "      <td>999.23</td>\n",
       "      <td>893.75</td>\n",
       "      <td>894.85</td>\n",
       "      <td>727384900</td>\n",
       "      <td>6.565445e+09</td>\n",
       "      <td>-0.089118</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.089118</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-05-22</th>\n",
       "      <td>1354.88</td>\n",
       "      <td>1248.97</td>\n",
       "      <td>1300.98</td>\n",
       "      <td>1232.20</td>\n",
       "      <td>1235.22</td>\n",
       "      <td>667319500</td>\n",
       "      <td>8.722466e+09</td>\n",
       "      <td>-0.088318</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.088318</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-08-17</th>\n",
       "      <td>1168.03</td>\n",
       "      <td>1152.67</td>\n",
       "      <td>1152.67</td>\n",
       "      <td>1068.66</td>\n",
       "      <td>1070.41</td>\n",
       "      <td>536182000</td>\n",
       "      <td>3.656666e+09</td>\n",
       "      <td>-0.083577</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.083577</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-02-27</th>\n",
       "      <td>3040.60</td>\n",
       "      <td>3048.83</td>\n",
       "      <td>3049.77</td>\n",
       "      <td>2763.39</td>\n",
       "      <td>2771.79</td>\n",
       "      <td>16137566300</td>\n",
       "      <td>1.290000e+11</td>\n",
       "      <td>-0.088407</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.088407</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-06-04</th>\n",
       "      <td>4000.74</td>\n",
       "      <td>3981.82</td>\n",
       "      <td>3987.27</td>\n",
       "      <td>3659.09</td>\n",
       "      <td>3670.40</td>\n",
       "      <td>11485935700</td>\n",
       "      <td>1.460000e+11</td>\n",
       "      <td>-0.082570</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.082570</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-07-27</th>\n",
       "      <td>4070.91</td>\n",
       "      <td>3985.57</td>\n",
       "      <td>4051.16</td>\n",
       "      <td>3720.44</td>\n",
       "      <td>3725.56</td>\n",
       "      <td>55600324700</td>\n",
       "      <td>7.210000e+11</td>\n",
       "      <td>-0.084834</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.084834</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-08-24</th>\n",
       "      <td>3507.74</td>\n",
       "      <td>3373.48</td>\n",
       "      <td>3388.36</td>\n",
       "      <td>3191.88</td>\n",
       "      <td>3209.91</td>\n",
       "      <td>33467179200</td>\n",
       "      <td>3.590000e+11</td>\n",
       "      <td>-0.084907</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.084907</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose     Open  Highest   Lowest    Close       Volume  \\\n",
       "Day                                                                     \n",
       "1996-12-16   1110.04  1005.01  1005.12   999.63  1000.02    143300300   \n",
       "1996-12-17   1000.02   907.65   943.37   903.85   905.58    601468400   \n",
       "1997-02-18    982.40   980.48   999.23   893.75   894.85    727384900   \n",
       "1997-05-22   1354.88  1248.97  1300.98  1232.20  1235.22    667319500   \n",
       "1998-08-17   1168.03  1152.67  1152.67  1068.66  1070.41    536182000   \n",
       "2007-02-27   3040.60  3048.83  3049.77  2763.39  2771.79  16137566300   \n",
       "2007-06-04   4000.74  3981.82  3987.27  3659.09  3670.40  11485935700   \n",
       "2015-07-27   4070.91  3985.57  4051.16  3720.44  3725.56  55600324700   \n",
       "2015-08-24   3507.74  3373.48  3388.36  3191.88  3209.91  33467179200   \n",
       "\n",
       "                   Money    Return  Reyurn_2  Return_2  diff  \n",
       "Day                                                           \n",
       "1996-12-16  1.378386e+09 -0.099114         0 -0.099114   0.0  \n",
       "1996-12-17  5.295694e+09 -0.094438         0 -0.094438   0.0  \n",
       "1997-02-18  6.565445e+09 -0.089118         0 -0.089118   0.0  \n",
       "1997-05-22  8.722466e+09 -0.088318         0 -0.088318   0.0  \n",
       "1998-08-17  3.656666e+09 -0.083577         0 -0.083577   0.0  \n",
       "2007-02-27  1.290000e+11 -0.088407         0 -0.088407   0.0  \n",
       "2007-06-04  1.460000e+11 -0.082570         0 -0.082570   0.0  \n",
       "2015-07-27  7.210000e+11 -0.084834         0 -0.084834   0.0  \n",
       "2015-08-24  3.590000e+11 -0.084907         0 -0.084907   0.0  "
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new[data_new['Return']<-0.08]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算上证综指月度，季度，年度的收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 月度收益率t表示月份"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "月度收益率\n",
    "$t$表示月份\n",
    "$$\n",
    "R_{t}=\\frac{P_{close,t}-P_{close,t-1}}{P_{close,t-1}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 算法1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.data_new日度数据里选择月的最后一天close 2.把这个close更改一下变成上个月的最后一天3.计算月度收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.2900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-31</th>\n",
       "      <td>537.3500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-02-29</th>\n",
       "      <td>552.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>556.3900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-04-30</th>\n",
       "      <td>681.1600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>344 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close\n",
       "Day                  \n",
       "1995-12-31   555.2900\n",
       "1996-01-31   537.3500\n",
       "1996-02-29   552.9400\n",
       "1996-03-31   556.3900\n",
       "1996-04-30   681.1600\n",
       "...               ...\n",
       "2024-03-31  3041.1669\n",
       "2024-04-30  3104.8245\n",
       "2024-05-31  3086.8134\n",
       "2024-06-30  2967.4028\n",
       "2024-07-31  2938.7493\n",
       "\n",
       "[344 rows x 1 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data=data_new.resample('ME')['Close'].last().to_frame()\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-31</th>\n",
       "      <td>537.3500</td>\n",
       "      <td>555.2900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-02-29</th>\n",
       "      <td>552.9400</td>\n",
       "      <td>537.3500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>556.3900</td>\n",
       "      <td>552.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-04-30</th>\n",
       "      <td>681.1600</td>\n",
       "      <td>556.3900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "      <td>3015.1712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "      <td>3041.1669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "      <td>3104.8245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "      <td>3086.8134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2967.4028</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>344 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose\n",
       "Day                             \n",
       "1995-12-31   555.2900        NaN\n",
       "1996-01-31   537.3500   555.2900\n",
       "1996-02-29   552.9400   537.3500\n",
       "1996-03-31   556.3900   552.9400\n",
       "1996-04-30   681.1600   556.3900\n",
       "...               ...        ...\n",
       "2024-03-31  3041.1669  3015.1712\n",
       "2024-04-30  3104.8245  3041.1669\n",
       "2024-05-31  3086.8134  3104.8245\n",
       "2024-06-30  2967.4028  3086.8134\n",
       "2024-07-31  2938.7493  2967.4028\n",
       "\n",
       "[344 rows x 2 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['Preclose']=Month_data['Close'].shift(1)\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-31</th>\n",
       "      <td>537.3500</td>\n",
       "      <td>555.2900</td>\n",
       "      <td>-0.032307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-02-29</th>\n",
       "      <td>552.9400</td>\n",
       "      <td>537.3500</td>\n",
       "      <td>0.029013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>556.3900</td>\n",
       "      <td>552.9400</td>\n",
       "      <td>0.006239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-04-30</th>\n",
       "      <td>681.1600</td>\n",
       "      <td>556.3900</td>\n",
       "      <td>0.224249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "      <td>3015.1712</td>\n",
       "      <td>0.008622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "      <td>3041.1669</td>\n",
       "      <td>0.020932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "      <td>3104.8245</td>\n",
       "      <td>-0.005801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "      <td>3086.8134</td>\n",
       "      <td>-0.038684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2967.4028</td>\n",
       "      <td>-0.009656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>344 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose    Return\n",
       "Day                                       \n",
       "1995-12-31   555.2900        NaN       NaN\n",
       "1996-01-31   537.3500   555.2900 -0.032307\n",
       "1996-02-29   552.9400   537.3500  0.029013\n",
       "1996-03-31   556.3900   552.9400  0.006239\n",
       "1996-04-30   681.1600   556.3900  0.224249\n",
       "...               ...        ...       ...\n",
       "2024-03-31  3041.1669  3015.1712  0.008622\n",
       "2024-04-30  3104.8245  3041.1669  0.020932\n",
       "2024-05-31  3086.8134  3104.8245 -0.005801\n",
       "2024-06-30  2967.4028  3086.8134 -0.038684\n",
       "2024-07-31  2938.7493  2967.4028 -0.009656\n",
       "\n",
       "[344 rows x 3 columns]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['Return']=(Month_data['Close']/Month_data['Preclose'])-1\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2008-10-31</th>\n",
       "      <td>1728.79</td>\n",
       "      <td>2293.78</td>\n",
       "      <td>-0.246314</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Close  Preclose    Return\n",
       "Day                                    \n",
       "2008-10-31  1728.79   2293.78 -0.246314"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data[Month_data['Return']==Month_data['Return'].min()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 算法2\n",
    "$$\n",
    "R_t=\\prod(R_{t,d}+1)-1\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Return_plus1</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>0.866098</td>\n",
       "      <td>-0.133902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-31</th>\n",
       "      <td>0.967693</td>\n",
       "      <td>-0.032307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-02-29</th>\n",
       "      <td>1.029013</td>\n",
       "      <td>0.029013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>1.006239</td>\n",
       "      <td>0.006239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-04-30</th>\n",
       "      <td>1.224249</td>\n",
       "      <td>0.224249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>1.008622</td>\n",
       "      <td>0.008622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>1.020932</td>\n",
       "      <td>0.020932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>0.994199</td>\n",
       "      <td>-0.005801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>0.961316</td>\n",
       "      <td>-0.038684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>0.990344</td>\n",
       "      <td>-0.009656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>344 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Return_plus1    Return\n",
       "Day                               \n",
       "1995-12-31      0.866098 -0.133902\n",
       "1996-01-31      0.967693 -0.032307\n",
       "1996-02-29      1.029013  0.029013\n",
       "1996-03-31      1.006239  0.006239\n",
       "1996-04-30      1.224249  0.224249\n",
       "...                  ...       ...\n",
       "2024-03-31      1.008622  0.008622\n",
       "2024-04-30      1.020932  0.020932\n",
       "2024-05-31      0.994199 -0.005801\n",
       "2024-06-30      0.961316 -0.038684\n",
       "2024-07-31      0.990344 -0.009656\n",
       "\n",
       "[344 rows x 2 columns]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return_plus1']=data_new['Return']+1\n",
    "Month_data2=data_new.resample('ME')['Return_plus1'].prod().to_frame()\n",
    "Month_data2['Return']=Month_data2['Return_plus1']-1\n",
    "Month_data2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 年度收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\宁\\AppData\\Local\\Temp\\ipykernel_9036\\188533854.py:1: FutureWarning: 'Y' is deprecated and will be removed in a future version, please use 'YE' instead.\n",
      "  Year_data=data_new.resample('Y')['Close'].last().to_frame()\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>917.0200</td>\n",
       "      <td>555.2900</td>\n",
       "      <td>0.651425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>1194.1000</td>\n",
       "      <td>917.0200</td>\n",
       "      <td>0.302153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>1146.7000</td>\n",
       "      <td>1194.1000</td>\n",
       "      <td>-0.039695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>1366.5800</td>\n",
       "      <td>1146.7000</td>\n",
       "      <td>0.191750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>2073.4800</td>\n",
       "      <td>1366.5800</td>\n",
       "      <td>0.517277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>1645.9700</td>\n",
       "      <td>2073.4800</td>\n",
       "      <td>-0.206180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.6500</td>\n",
       "      <td>1645.9700</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>1497.0400</td>\n",
       "      <td>1357.6500</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>1266.5000</td>\n",
       "      <td>1497.0400</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1266.5000</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>2675.4700</td>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>5261.5600</td>\n",
       "      <td>2675.4700</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>1820.8100</td>\n",
       "      <td>5261.5600</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>3277.1400</td>\n",
       "      <td>1820.8100</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>2808.0800</td>\n",
       "      <td>3277.1400</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>2199.4200</td>\n",
       "      <td>2808.0800</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>2269.1300</td>\n",
       "      <td>2199.4200</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>2115.9800</td>\n",
       "      <td>2269.1300</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>3234.6800</td>\n",
       "      <td>2115.9800</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3539.1800</td>\n",
       "      <td>3234.6800</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>3103.6400</td>\n",
       "      <td>3539.1800</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>3307.1700</td>\n",
       "      <td>3103.6400</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>2493.9000</td>\n",
       "      <td>3307.1700</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>3050.1200</td>\n",
       "      <td>2493.9000</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>3473.0700</td>\n",
       "      <td>3050.1200</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>3639.7800</td>\n",
       "      <td>3473.0700</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>3089.2579</td>\n",
       "      <td>3639.7800</td>\n",
       "      <td>-0.151251</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>2974.9348</td>\n",
       "      <td>3089.2579</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2974.9348</td>\n",
       "      <td>-0.012163</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose    Return\n",
       "Day                                       \n",
       "1995-12-31   555.2900        NaN       NaN\n",
       "1996-12-31   917.0200   555.2900  0.651425\n",
       "1997-12-31  1194.1000   917.0200  0.302153\n",
       "1998-12-31  1146.7000  1194.1000 -0.039695\n",
       "1999-12-31  1366.5800  1146.7000  0.191750\n",
       "2000-12-31  2073.4800  1366.5800  0.517277\n",
       "2001-12-31  1645.9700  2073.4800 -0.206180\n",
       "2002-12-31  1357.6500  1645.9700 -0.175167\n",
       "2003-12-31  1497.0400  1357.6500  0.102670\n",
       "2004-12-31  1266.5000  1497.0400 -0.153997\n",
       "2005-12-31  1161.0600  1266.5000 -0.083253\n",
       "2006-12-31  2675.4700  1161.0600  1.304334\n",
       "2007-12-31  5261.5600  2675.4700  0.966593\n",
       "2008-12-31  1820.8100  5261.5600 -0.653941\n",
       "2009-12-31  3277.1400  1820.8100  0.799825\n",
       "2010-12-31  2808.0800  3277.1400 -0.143131\n",
       "2011-12-31  2199.4200  2808.0800 -0.216753\n",
       "2012-12-31  2269.1300  2199.4200  0.031695\n",
       "2013-12-31  2115.9800  2269.1300 -0.067493\n",
       "2014-12-31  3234.6800  2115.9800  0.528691\n",
       "2015-12-31  3539.1800  3234.6800  0.094136\n",
       "2016-12-31  3103.6400  3539.1800 -0.123062\n",
       "2017-12-31  3307.1700  3103.6400  0.065578\n",
       "2018-12-31  2493.9000  3307.1700 -0.245911\n",
       "2019-12-31  3050.1200  2493.9000  0.223032\n",
       "2020-12-31  3473.0700  3050.1200  0.138667\n",
       "2021-12-31  3639.7800  3473.0700  0.048001\n",
       "2022-12-31  3089.2579  3639.7800 -0.151251\n",
       "2023-12-31  2974.9348  3089.2579 -0.037007\n",
       "2024-12-31  2938.7493  2974.9348 -0.012163"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Year_data=data_new.resample('Y')['Close'].last().to_frame()\n",
    "Year_data['Preclose']=Year_data['Close'].shift(1)\n",
    "Year_data['Return']=(Year_data['Close']/Year_data['Preclose'])-1\n",
    "Year_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.12609727342069055"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_return=Year_data['Return'].mean()\n",
    "mean_return"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
